package com.kangtushizhe.controller;

import com.kangtushizhe.emuns.Drug;
import com.kangtushizhe.emuns.Prescription;
import com.kangtushizhe.entities.CommonResult;
import com.kangtushizhe.service.PrescriptionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-23
 */
@RestController
@RequestMapping("/prescription")
@Tag(name = "处方")
public class PrescriptionController {
    @Resource
    private PrescriptionService prescriptionService;

    //获取所有处方
    @GetMapping("/all")
    @Operation(summary = "获取所有处方", description = "() => 获取所有处方")
    public CommonResult getAllPrescription() {
        List<Prescription> allPrescription = prescriptionService.list();
        return new CommonResult(200, "获取所有处方", allPrescription);
    }

    //创建新处方
    @PostMapping("/")
    @Operation(summary = "创建新处方", description = "(Integer doctorId,Integer pharmacistId, String drugIdList,Integer number,String wUsage,String creationTime) => 创建新处方")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "doctorId", description = "医生id", required = true),
            @Parameter(name = "pharmacistId", description = "药师id", required = true),
            @Parameter(name = "drugIdList", description = "药物列表", required = true),
            @Parameter(name = "number", description = "数量", required = true),
            @Parameter(name = "wUsage", description = "药物用法", required = true),
            @Parameter(name = "creationTime", description = "处方创建时间", required = true),
    })
    public CommonResult createCart(@RequestParam(required = true) Integer doctorId,
                                   @RequestParam(required = true) Integer pharmacistId,
                                   @RequestParam(required = true) String drugIdList,
                                   @RequestParam(required = true) Integer number,
                                   @RequestParam(required = true) String wUsage,
                                   @RequestParam(required = true) String creationTime
                                   ) {
        prescriptionService.save(new Prescription(null,doctorId,pharmacistId,drugIdList,number,wUsage,creationTime));
        return new CommonResult(200, "创建处方成功");
    }

    //根据id获取处方
    @GetMapping("/id")
    @Operation(summary = "根据id获取处方", description = "() => 获取指定处方")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult getPrescriptionById(@RequestParam Integer id) {
        Prescription prescription = prescriptionService.getById(id);
        return new CommonResult(200, "根据id获取处方", prescription);
    }

    //根据id更新处方信息
    @PutMapping("/id")
    @Operation(summary = "根据id更新处方信息", description = "(Integer id,?Integer doctorId,?Integer pharmacistId, ?String drugIdList,?Integer number,?String wUsage,?String creationTime) => 更新处方信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "doctorId", description = "医生id", required = false),
            @Parameter(name = "pharmacistId", description = "药师id", required = false),
            @Parameter(name = "drugIdList", description = "药物列表", required = false),
            @Parameter(name = "number", description = "数量", required = false),
            @Parameter(name = "wUsage", description = "药物用法", required = false),
            @Parameter(name = "creationTime", description = "处方创建时间", required = false),
    })
    public CommonResult getCartById(@RequestParam Integer id,
                                    @RequestParam(required = false) Integer doctorId,
                                    @RequestParam(required = false) Integer pharmacistId,
                                    @RequestParam(required = false) String drugIdList,
                                    @RequestParam(required = false) Integer number,
                                    @RequestParam(required = false) String wUsage,
                                    @RequestParam(required = false) String creationTime) {
        Prescription prescription = prescriptionService.getById(id);
        if (prescription != null) {
            prescriptionService.updateById(new Prescription(id,doctorId,pharmacistId,drugIdList,number,wUsage,creationTime));
            return new CommonResult(200, "根据id:" + id + ",更新处方信息成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到处方");
        }
    }

    //根据id移除处方信息
    @DeleteMapping("/id")
    @Operation(summary = "根据id移除处方信息", description = "(Integer id) => 根据id移除处方信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult removeCartById(@RequestParam(required = true) Integer id) {
        Prescription prescription = prescriptionService.getById(id);
        if (prescription != null) {
            prescriptionService.removeById(id);
            return new CommonResult(200, "根据id:" + id + ",移除药物成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到药物");
        }
    }
}
